package com.mp.approvalreq.util;

import com.mp.approvalreq.entity.Outdoor;
import com.plusmoney.util.DateTimeHelper;

import java.util.*;
import java.util.stream.Collectors;

public class OutdoorUtils {

    public static Map<Integer, Outdoor> getOutdoorMap(List<Outdoor> list) {
        return list.stream().collect(Collectors.toMap(Outdoor::getRequestId, outdoor -> outdoor, (a, b) -> b));
    }

    public static List<Integer> getRequestIds(List<Outdoor> outdoors) {
        return outdoors.stream().map(Outdoor::getRequestId).collect(Collectors.toList());
    }

    public static Integer calculateDuration(Outdoor outdoor) {
        try {
            Date start = DateTimeHelper.parse(outdoor.getStartTime(), DateTimeHelper.YEAR_MONTH_DAY_HOUR_MINUTE_PATTERN);
            Date end = DateTimeHelper.parse(outdoor.getEndTime(), DateTimeHelper.YEAR_MONTH_DAY_HOUR_MINUTE_PATTERN);

            return Math.toIntExact((end.getTime() - start.getTime()) / 1000);
        } catch (Exception e) {
            EmailUtils.sendMail("初始化外出时长出错", outdoor);
            return 0;
        }
    }
}
